package com.csi.controller;

import com.csi.domain.Hotel;
import com.csi.domain.RoomType;
import com.csi.domain.vo.RoomTypeVo;
import com.csi.mapper.HotelMapper;
import com.csi.domain.HotelLocation;
import com.csi.service.HotelService;
import com.csi.service.UserBookService;
import com.csi.utils.JsonResult;
import com.csi.utils.JwtUtil;
import com.csi.utils.ReturnResults;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hotel")
@Tag(name="Hotel",description = "酒店相关接口")
@JsonIgnoreProperties(ignoreUnknown = true)
public class HotelController {

    @Autowired
    private HotelService hotelService;
    @Autowired
    private UserBookService userBookService;


    /**
     * 新增酒店 -酒店表-酒店位置表-酒店信息表
     * @param hotel 酒店对象(里面封装了酒店位置对象)
     * @return 执行结果
     */
    @PostMapping("/save" )
    @Operation(summary = "酒店入驻",description = "酒店入驻")
    public Object save(Hotel hotel,
                       @RequestParam("hotelImage") MultipartFile[] multipartFiles,
                       @RequestParam("hotelLocation.provinceLocation") String provinceLocation,
                       @RequestParam("hotelLocation.cityLocation") String cityLocation,
                       @RequestParam("hotelLocation.regionLocation") String regionLocation){

        // 打印接收到的数据，帮助调试
        System.out.println("hotel: " + hotel);
        System.out.println("hotelImage count: " + multipartFiles.length);
        HotelLocation hotelLocation = new HotelLocation();
        hotelLocation.setProvinceLocation(provinceLocation);
        hotelLocation.setCityLocation(cityLocation);
        hotelLocation.setRegionLocation(regionLocation);

        // 设置到 hotel 对象中
        hotel.setHotelLocation(hotelLocation);

        int saved = hotelService.save(hotel,multipartFiles);
        if (saved > 0){
            return new JsonResult<>(ReturnResults.ACCESS_SUCCESS,"保存成功");
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"保存失败");
    }



    /**
     * 热门酒店推荐
     * @return
     */
    @GetMapping("/hotHotel")
    @Operation(summary = "热门酒店",description = "热门酒店推荐渲染")
    public Object hotHotel() {
        List<Hotel> hotelList= hotelService.hotHotel();
        if(hotelList.size()>0){
            return new JsonResult<>(ReturnResults.ACCESS_SUCCESS,hotelList,"查询成功");
        }else {
            return new JsonResult<>(ReturnResults.ACCESS_FAILED,"查询失败");
        }
    }

    @GetMapping("/findAll")
    @Operation(summary = "全部酒店",description = "")
    public Object findAll() {
        List<Hotel> hotelList = hotelService.findAll();
        if(hotelList.size()>0){
            return new JsonResult<>(ReturnResults.ACCESS_SUCCESS,hotelList,"查询成功");
        }else {
            return new JsonResult<>(ReturnResults.ACCESS_FAILED,"查询失败");
        }
    }

    /**
     * 首页多条件查询酒店
     * @param peopleNum
     * @param city_location
     * @param check_time
     * @param leave_time
     * @return
     */
    @GetMapping("/like4book")
    @Operation(summary = "首页多条件查询",description = "酒店首页多条件查询酒店")
    public Object like4book(@RequestParam(value="people_num",required=false) Integer peopleNum,
                            @RequestParam(value="city_location",required=false) String city_location,
                            @RequestParam(value="check_time",required=false) String check_time,
                            @RequestParam(value="leave_time",required=false) String leave_time) {
        List<Hotel> hotels = hotelService.like4book(peopleNum, city_location, check_time, leave_time);
        if (hotels != null && !hotels.isEmpty()) {
            return new JsonResult<>(ReturnResults.ACCESS_SUCCESS,hotels,"查询成功");
        }
        return new JsonResult<>( ReturnResults.ACCESS_FAILED,"没有该酒店");
    }

    /**
     * 查询酒店详情
     * @param hotelId
     * @return
     */
    @GetMapping("/findHotelListById")
    @Operation(summary = "酒店详情",description = "酒店详情页信息")
    public Object findHotelListById(@RequestParam(value="id") long hotelId) {
        Map<String, Object> hotelListById = hotelService.findHotelListById(hotelId);
        if (hotelListById != null){
            return new JsonResult<>(ReturnResults.ACCESS_SUCCESS,hotelListById,"查询成功");
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"查询失败");
    }

    /**
     * 查询房间类型
     * @param hotelId 酒店id
     * @param checkTime 入住时间
     * @param leaveTime 离店时间
     * @return Map<String,Object>
     */
    @GetMapping("/findRoomTypeById")
    @Operation(summary = "酒店房间",description = "酒店详情页的房间列表")
    public Object findRoomTypeById(@RequestParam(value = "hotelId")long hotelId, @RequestParam(value = "checkTime",required = false)String checkTime, @RequestParam(value = "leaveTime",required = false)String leaveTime ){
        List<Map<String, Object>> roomType = hotelService.findRoomTypeById(hotelId, checkTime, leaveTime);
        System.out.println(roomType);
        if (roomType != null){
            return new JsonResult<>(roomType);
        }
        return new JsonResult<>();
    }

    @GetMapping("/findHotelByCity")
    public Object findHotelByCity(@RequestParam("cityName") String cityName){
        List<Hotel> hotels = hotelService.findHotelByCity(cityName);
        if (hotels != null){
            return new JsonResult<>(hotels);
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"查询失败");
    }

    @RequestMapping("/findRoomNum")
    public Object findRoomNum(@RequestParam(value = "roomTypeId") int roomTypeId, @RequestParam(value = "checkTime")String checkTime, @RequestParam(value = "leaveTime")String leaveTime){

        int roomNum = userBookService.findRoomNum(roomTypeId, checkTime, leaveTime);
        if (roomNum > 0){
            return new JsonResult<>(roomNum);
        }
        return new JsonResult<>();
    }

    //返回list集合，集合中使用vo
    @RequestMapping("/roomTypeLastNum")
    public Object roomTypeLastNum(@RequestBody RoomTypeVo roomTypesVo){
                //现在要拿到roomTypesVo里的房型id列表去查看相对应的空闲房间，然后放到map中
        List<Integer> roomTypeIds = roomTypesVo.getRoomTypeIds();
        Map<String,Integer> map = new HashMap<>();

        for (Integer roomTypeId : roomTypeIds) {
            //根据roomTypeId查询对应剩余
            Integer roomNum = userBookService.findRoomNum(roomTypeId, roomTypesVo.getCheckTime(), roomTypesVo.getLeaveTime());
            map.put(String.valueOf(roomTypeId),roomNum);
            roomTypesVo.setRoomTypeNumMap(map);
        }

        return new JsonResult<>(ReturnResults.ACCESS_SUCCESS,map,"success！");

    }


    /**
     * 根据用户ID查看所有酒店订单
     * @param request
     * @return
     */
    @GetMapping("/findHotelByUserId")
    @Operation(summary = "查找所有订单", description = "根据用户Id查找所有订单")
    public Object findHotelByUserId(HttpServletRequest request){
        String token = request.getHeader("token");
        long userId = JwtUtil.getTokenBody(token).get("userId", Long.class);
        List<Map<String, Object>>  hotels = hotelService.findHotelByUserId(userId);
        if (hotels !=null){
            return new JsonResult<>(hotels);
        }
        return new JsonResult<>();
    }

    @GetMapping("/findBillByLike")
    @Operation(summary = "条件查询订单", description = "查找不同状态的订单")
    public Object findBillByLike(HttpServletRequest request,@RequestParam("status") String status){
        String token = request.getHeader("token");
        long userId = JwtUtil.getTokenBody(token).get("userId", Long.class);
        List<Map<String, Object>>  hotels = hotelService.findBillByLike(userId,status);
        if (hotels !=null){
            return new JsonResult<>(hotels);
        }
        return new JsonResult<>();
    }
    /**
     *根据id查找酒店名字
     * @param request 请求token
     * @return 执行结果
     */
    @GetMapping("/findHotelNameById")
    public Object findHotelName(HttpServletRequest request){
        String token = request.getHeader("token");
        long hotelId = JwtUtil.getTokenBody(token).get("hotelId", Long.class);
        String hotelName = hotelService.findHotelName(hotelId);
        if (hotelName != null){
            return new JsonResult<>(hotelName);
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"没有该酒店");
    }

    /**
     * 特价酒店
     * @return
     */
    @GetMapping("/discountedhotel")
    @Operation(summary = "特价酒店",description = "特价酒店")
    public Object discountedHotel(){
        List<Hotel> hotels=hotelService.discountedhotel();
        if(hotels != null && !hotels.isEmpty()) {
            return new JsonResult<>(hotels);
        }else{
            return new JsonResult<>( ReturnResults.ACCESS_FAILED,"查询失败");
        }
    }
}
